package com.kefu.boss.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kefu.boss.model.BossUser;
import com.kefu.boss.service.BossRoleService;
import com.kefu.boss.service.BossUserService;
import com.kefu.boss.vo.BossUserInsertVo;
import com.kefu.boss.vo.BossUserSearchVo;
import com.kefu.boss.vo.BossUserUpdateVo;
import com.kefu.boss.vo.BossUserVo;
import com.kefu.common.controller.AbstractBaseController;
import com.kefu.common.result.Result;
import com.kefu.common.result.ResultGenerator;
import com.kefu.common.vo.DeleteVo;
import com.kefu.common.vo.DetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

@Api(tags = "业务支撑用户")
@RestController
@RequestMapping("boss/user")
public class BossUserController extends AbstractBaseController {
    @Resource
    protected BossRoleService bossRoleService;
    @Resource
    protected BossUserService bossUserService;
    
    @PostMapping("/add")
    @ApiOperation(value = "新增", notes = "新增")
    public Result<String> add(@Validated @RequestBody BossUserInsertVo bossUserVo) {
    	logger.info("新增:{}", bossUserVo);
    	
        bossUserService.addUser(bossUserVo);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除", notes = "删除")
    public Result<String> delete(@Validated @RequestBody DeleteVo delete) {
    	logger.info("删除:{}", delete);
    	
        bossUserService.deleteById(delete.getId());
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改", notes = "修改")
    public Result<String> update(@Validated @RequestBody BossUserUpdateVo bossUserVo) {
    	logger.info("修改:{}", bossUserVo);
    	
        bossUserService.updateUser(bossUserVo);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    @ApiOperation(value = "详情", notes = "详情")
    public Result<BossUser> detail(@Validated @RequestBody DetailVo detail) {
        BossUser bossUser = bossUserService.findById(detail.getId());
        if (Objects.nonNull(bossUser)) {
            bossUser.setRoleList(bossRoleService.getByUserId(bossUser.getUserId()));
        }
        return ResultGenerator.genSuccessResult(bossUser);
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询", notes = "查询")
    public Result<Page<BossUserVo>> list(@Validated @RequestBody BossUserSearchVo search) {

        Page<BossUser> page = bossUserService.searchLambdaPage(search);
        // 转换page为vo
        Page<BossUserVo> voPage = new Page<>();
        voPage.setTotal(page.getTotal());
        voPage.setRecords(BossUserVo.convert(page.getRecords()));
        voPage.setCurrent(page.getCurrent());
        voPage.setSize(page.getSize());
        // 属性补充：角色列表
        bossUserService.fillRoleList(voPage.getRecords());


        return ResultGenerator.genSuccessResult(voPage);

    }
}
